package com.baizhi.service.impl;

import com.baizhi.entity.User;
import com.baizhi.mapper.UserMapper;
import com.baizhi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;
    @Override
    public PageInfo<User> showUserPage(Integer pageNum, Integer pageSize) {
        return new PageInfo<>(mapper.selectAllUser(pageNum,pageSize),10);
    }

    @Override
    public User showById(Integer id) {
        return mapper.selectUserById(id);
    }

    @Override
    public void updateUser(User user) {
        mapper.updateUser(user);
    }

    @Override
    public Boolean userLogin(User user) {
        User user1 = mapper.selectUserByEmail(user.getEmail());
        if(user1.getStatus()!=1){
            throw new RuntimeException("该账户处于锁定状态");
        }
        if(user1!=null&&user.getPassword().equals(user1.getPassword())){
            return true;
        }else {
            throw new RuntimeException("用户账号或密码错误");
        }
    }

    @Override
    public User selectUserByEmail(String email) {
        return mapper.selectUserByEmail(email);
    }
    public Boolean userRegister(User user,String pwd){
        if(mapper.selectUserByEmail(user.getEmail())!=null){
            throw new RuntimeException("该邮箱已被注册");
        }
        if(user.getPassword().equals(pwd)){
            mapper.addUser(user);
            return true;
        }else {
            throw new RuntimeException("注册失败，两次密码不一致");
        }
    }
}
